import 'package:flutter/cupertino.dart';

import '../storage/get_storage.dart';

class Auth {
  // user auth info
  static String getLoginKey() {
    return AppStorage().readKey(StorageKey.token) ?? '';
  }

  static void setToken(String token) {
    AppStorage().writeKey(StorageKey.token, token);
  }

  static String getUserId() {
    return AppStorage().readKey(StorageKey.userId) ?? '';
  }

  static String getPhone() {
    return AppStorage().readKey(StorageKey.phone) ?? '';
  }

  static void updateAuthInfo(Map detail) {
    String token = detail[StorageKey.token].toString();
    if (token != '') {
      AppStorage().writeKey(StorageKey.token, token);
    }

    String userId = detail[StorageKey.userId].toString();
    if (userId != '') {
      AppStorage().writeKey(StorageKey.userId, userId);
    }

    String phone = detail[StorageKey.phone].toString();
    if (phone != '') {
      AppStorage().writeKey(StorageKey.phone, phone);
    }

    debugPrint('=== updateAuthInfo: $detail');
  }

  static void login(Map detail) {
    updateAuthInfo(detail);
  }

  static void logout() {
    AppStorage().removeKey(StorageKey.userId);
    AppStorage().removeKey(StorageKey.token);
    AppStorage().removeKey(StorageKey.phone);
  }

  static bool isLogin() {
    return (AppStorage().readKey(StorageKey.token) ?? '').toString().isNotEmpty;
  }
}